<?php
/**
 * Smarty plugin
 * 
 * $Id: modifier.rewrite.php 121 2010-05-28 07:57:29Z Administrator $
 * $Revision: 121 $
 * $Author: Administrator $
 * $Date: 2010-05-28 15:57:29 +0800 (星期五, 28 五月 2010) $
 * @package fastlib
 * @subpackage smarty
 */
/**
 * Smarty urlrewrite modifier plugin
 *
 * Type:	modifier<br />
 * Name:	rewrite<br />
 * 
 * 规则字串中的%ac%,%m%,%mGet%,%aGET%,%control%
 * 分别被替换成$ac,$m,$mGet,$aGet,$control
 * $argv可以是字符串或是关联数组 
 * 
 * @param mixed $agrv 附加参数
 * @param string $ac 模块动作
 * @param string $m 模块名
 * @param string $mGet 通过GET传模块名的GET名
 * @param string $aGet 通过GET传动作的GET名
 * @param string $control 控制脚本名
 * @return string改写好的URL规则
 */
function smarty_modifier_rewrite($agrv = '', $ac = __FASTMVC_ACTV__, $m = __FASTMVC_MV__, $mGet = __FASTMVC_M__, $aGet = __FASTMVC_ACT__, $control = __FASTMVC_ROOT__) {
	global $flibcfg;
	$rewriteRule = isset ( $flibcfg ['rewriteRule'] ) ? $flibcfg ['rewriteRule'] : '';
	if ($rewriteRule === '') {
		$rewriteRule = $control . '?' . http_build_query ( array ($mGet => $m, $aGet => $ac ) );
		if (is_string ( $agrv ) && $agrv != '') {
			$rewriteRule .= '&amp;' . strtr ( $agrv, array ('&' => '&amp;' ) );
		} elseif (is_array ( $agrv ) && count ( $agrv )) {
			$rewriteRule .= '&amp;' . http_build_query ( $agrv );
		}
	} else {
		if (is_string ( $agrv ) && $agrv != '') {
			$rewriteagrv = strtr ( $agrv, array ('&' => '&amp;' ) );
		} elseif (is_array ( $agrv ) && count ( $agrv )) {
			$rewriteagrv = http_build_query ( $agrv );
		} else {
			$rewriteagrv = '';
		}
		$rewriteRule = strtr ( $rewriteRule, array ('%ac%' => $ac, '%m%' => $m, '%mGet%' => $mGet, '%aGet%' => $aGet, '%control%' => substr ( $control, 0, strrpos ( $control, '.' ) ), '%QUERY_STRING%' => $rewriteagrv ) );
	}
	return $rewriteRule;
}